package cn.doitedu.hbase.day02;

import cn.doitedu.hbase.day01.ConnectionUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.RegionLocator;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat2;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * 是将普工格式的数据转成Hbase底层的HFile格式
 *
 * 使用MR完成普通格式到HFile格式的转换
 *
 */
public class HFileProcessor {

    /**
     * 只有mapper没有reducer
     * mapper的功能就是读取数据，然后转成HFile格式
     */
    public static class HFileProcessorMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue> {


        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            String[] fields = value.toString().split(",");
            String id = fields[0];
            String name = fields[1];
            int age = Integer.parseInt(fields[2]);
            String gender = fields[3];
            String city = fields[4];
            int sal = Integer.parseInt(fields[5]);
            String grade = fields[6];
            //将数据输出
            context.write(new ImmutableBytesWritable(Bytes.toBytes(id)), new KeyValue(Bytes.toBytes(id), Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes(name)));
            context.write(new ImmutableBytesWritable(Bytes.toBytes(id)), new KeyValue(Bytes.toBytes(id), Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes(age)));
            context.write(new ImmutableBytesWritable(Bytes.toBytes(id)), new KeyValue(Bytes.toBytes(id), Bytes.toBytes("base_info"), Bytes.toBytes("gender"), Bytes.toBytes(gender)));
            context.write(new ImmutableBytesWritable(Bytes.toBytes(id)), new KeyValue(Bytes.toBytes(id), Bytes.toBytes("extra_info"), Bytes.toBytes("city"), Bytes.toBytes(city)));
            context.write(new ImmutableBytesWritable(Bytes.toBytes(id)), new KeyValue(Bytes.toBytes(id), Bytes.toBytes("extra_info"), Bytes.toBytes("sal"), Bytes.toBytes(sal)));
            context.write(new ImmutableBytesWritable(Bytes.toBytes(id)), new KeyValue(Bytes.toBytes(id), Bytes.toBytes("extra_info"), Bytes.toBytes("grade"), Bytes.toBytes(grade)));

        }
    }

    public static void main(String[] args) throws Exception {

        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        //设置程序的主类
        job.setJarByClass(HFileProcessor.class);

        //设置Mapper的类
        job.setMapperClass(HFileProcessorMapper.class);
        job.setNumReduceTasks(0);

        //设置Mapper输出数据的key-value类型
        job.setMapOutputKeyClass(ImmutableBytesWritable.class);
        job.setMapOutputValueClass(KeyValue.class);

        //设置数据输出的格式
        job.setOutputKeyClass(ImmutableBytesWritable.class);
        job.setOutputValueClass(KeyValue.class);

        //设置mapreduce的数据输入路径，输出路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //获取表的元数据信息
        Connection connection = ConnectionUtils.getConnection();
        TableName tableName = TableName.valueOf(args[2]);
        Table table = connection.getTable(tableName);
        //获取表region的信息
        RegionLocator regionLocator = connection.getRegionLocator(tableName);

        //设置数据数据格式，并且关联相关参数
        HFileOutputFormat2.configureIncrementalLoad(job, table, regionLocator);

        job.setOutputFormatClass(HFileOutputFormat2.class);

        //提交job等待完成
        job.waitForCompletion(true);


    }

}
